﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WhatCatch.Core2.Domain;
using WhatCatch.Core2.Model;
using WhatCatch.Core2.Repository;
using WhatCatch.Core2.UnitOfWork;

namespace WhatCatch.Core2
{
    public class CrawlerService
    {
        ICatchQueue _catchQueue;
        public CrawlerService(ICatchQueue catchQueue)
        {
            _catchQueue = catchQueue;
        }

        public bool AddTask(CatchTask task)
        {
            _catchQueue.TaskEnqueue(task);
            return true;
        }

        public void Run()
        {
            //IUnitOfWork unitOfWork = new EFUnitOfWork<DataContext>(new DataContext());
            //var repositoryCrawerPlan = unitOfWork.GetRepository<CrawlerPlan>();
            //var listCrawerPlan = repositoryCrawerPlan.Table.ToList();



            Task.Run(() =>
            {
                while (true)
                {
                    var result = _catchQueue.ResultDequeue();
                    if (result == null)
                    {
                        Thread.Sleep(1000);
                        continue;
                    }

                    //处理结果
                    if (result.DetailListResult != null)
                    {
                        //判重

                        //入库

                        foreach (var li in result.DetailListResult)
                        {
                            _catchQueue.TaskEnqueue(new CatchTask
                            {
                                TaskId = 0,
                                Url = li,
                                HandleType = 2,
                                ResourceId = 1,
                                Config = new ResourceConfig
                                {
                                    ListXPath = "",
                                    ListItemXPath = "//div[@class='list']/div[@class='list-li']",
                                    ListItemUrlXPath = "",
                                    PageXPath = "",
                                    PageItemXPath = "//div[@class='page']/a",
                                    PageItemUrlXPath = "",
                                    DetailXPath = "",
                                    DetailTitleXPath = "//div[@class='detail-title']",
                                }
                            });
                        }
                    }
                    if (result.PageListResult != null)
                    {
                        //判重

                        //入库

                        foreach (var li in result.PageListResult)
                        {
                            _catchQueue.TaskEnqueue(new CatchTask
                            {
                                TaskId = 0,
                                Url = li,
                                HandleType = 1,
                                ResourceId = 1,
                                Config = new ResourceConfig
                                {
                                    ListXPath = "",
                                    ListItemXPath = "//div[@class='list']/div[@class='list-li']",
                                    ListItemUrlXPath = "",
                                    PageXPath = "",
                                    PageItemXPath = "//div[@class='page']/a",
                                    PageItemUrlXPath = "",
                                    DetailXPath = "",
                                    DetailTitleXPath = "//div[@class='detail-title']",
                                }
                            });
                        }
                    }
                    if (result.DetailResult != null)
                    {
                        
                    }

                    Thread.Sleep(100);
                }
            });
        }
    }
}
